package cn.iocoder.yudao.module.obj.controller.admin.base;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.obj.controller.admin.base.vo.*;
import cn.iocoder.yudao.module.obj.dal.dataobject.base.ObjBaseInfoDO;
import cn.iocoder.yudao.module.obj.dal.dataobject.base.ObjDocInfoDO;
import cn.iocoder.yudao.module.obj.dal.dataobject.base.ObjDrmInfoDO;
import cn.iocoder.yudao.module.obj.dal.dataobject.base.ObjPlanInfoDO;
import cn.iocoder.yudao.module.obj.service.base.ObjBaseInfoService;

@Tag(name = "管理后台 - 项目基础信息表 ")
@RestController
@RequestMapping("/obj/base-info")
@Validated
public class ObjBaseInfoController {

    @Resource
    private ObjBaseInfoService baseInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建项目基础信息表 ")
    @PreAuthorize("@ss.hasPermission('obj:base-info:create')")
    public CommonResult<Long> createBaseInfo(@Valid @RequestBody ObjBaseInfoSaveReqVO createReqVO) {
        return success(baseInfoService.createBaseInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目基础信息表 ")
    @PreAuthorize("@ss.hasPermission('obj:base-info:update')")
    public CommonResult<Boolean> updateBaseInfo(@Valid @RequestBody ObjBaseInfoSaveReqVO updateReqVO) {
        baseInfoService.updateBaseInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目基础信息表 ")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('obj:base-info:delete')")
    public CommonResult<Boolean> deleteBaseInfo(@RequestParam("id") Long id) {
        baseInfoService.deleteBaseInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目基础信息表 ")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('obj:base-info:query')")
    public CommonResult<ObjBaseInfoRespVO> getBaseInfo(@RequestParam("id") Long id) {
        ObjBaseInfoDO baseInfo = baseInfoService.getBaseInfo(id);
        return success(BeanUtils.toBean(baseInfo, ObjBaseInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目基础信息表 分页")
    @PreAuthorize("@ss.hasPermission('obj:base-info:query')")
    public CommonResult<PageResult<ObjBaseInfoRespVO>> getBaseInfoPage(@Valid ObjBaseInfoPageReqVO pageReqVO) {
        PageResult<ObjBaseInfoDO> pageResult = baseInfoService.getBaseInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ObjBaseInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目基础信息表  Excel")
    @PreAuthorize("@ss.hasPermission('obj:base-info:export')")
    @OperateLog(type = EXPORT)
    public void exportBaseInfoExcel(@Valid ObjBaseInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ObjBaseInfoDO> list = baseInfoService.getBaseInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "项目基础信息表 .xls", "数据", ObjBaseInfoRespVO.class,
                        BeanUtils.toBean(list, ObjBaseInfoRespVO.class));
    }

    // ==================== 子表（项目文档信息表 ） ====================

    @GetMapping("/doc-info/page")
    @Operation(summary = "获得项目文档信息表 分页")
    @Parameter(name = "xmid", description = "项目编号")
    @PreAuthorize("@ss.hasPermission('obj:base-info:query')")
    public CommonResult<PageResult<ObjDocInfoDO>> getDocInfoPage(PageParam pageReqVO,
                                                                                        @RequestParam("xmid") String xmid) {
        return success(baseInfoService.getDocInfoPage(pageReqVO, xmid));
    }

    @PostMapping("/doc-info/create")
    @Operation(summary = "创建项目文档信息表 ")
    @PreAuthorize("@ss.hasPermission('obj:base-info:create')")
    public CommonResult<Long> createDocInfo(@Valid @RequestBody ObjDocInfoDO docInfo) {
        return success(baseInfoService.createDocInfo(docInfo));
    }

    @PutMapping("/doc-info/update")
    @Operation(summary = "更新项目文档信息表 ")
    @PreAuthorize("@ss.hasPermission('obj:base-info:update')")
    public CommonResult<Boolean> updateDocInfo(@Valid @RequestBody ObjDocInfoDO docInfo) {
        baseInfoService.updateDocInfo(docInfo);
        return success(true);
    }

    @DeleteMapping("/doc-info/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除项目文档信息表 ")
    @PreAuthorize("@ss.hasPermission('obj:base-info:delete')")
    public CommonResult<Boolean> deleteDocInfo(@RequestParam("id") Long id) {
        baseInfoService.deleteDocInfo(id);
        return success(true);
    }

	@GetMapping("/doc-info/get")
	@Operation(summary = "获得项目文档信息表 ")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('obj:base-info:query')")
	public CommonResult<ObjDocInfoDO> getDocInfo(@RequestParam("id") Long id) {
	    return success(baseInfoService.getDocInfo(id));
	}

    // ==================== 子表（项目资源设备信息表 ） ====================

    @GetMapping("/drm-info/page")
    @Operation(summary = "获得项目资源设备信息表 分页")
    @Parameter(name = "xmid", description = "项目编号")
    @PreAuthorize("@ss.hasPermission('obj:base-info:query')")
    public CommonResult<PageResult<ObjDrmInfoDO>> getDrmInfoPage(PageParam pageReqVO,
                                                                                        @RequestParam("xmid") String xmid) {
        return success(baseInfoService.getDrmInfoPage(pageReqVO, xmid));
    }

    @PostMapping("/drm-info/create")
    @Operation(summary = "创建项目资源设备信息表 ")
    @PreAuthorize("@ss.hasPermission('obj:base-info:create')")
    public CommonResult<Long> createDrmInfo(@Valid @RequestBody ObjDrmInfoDO drmInfo) {
        return success(baseInfoService.createDrmInfo(drmInfo));
    }

    @PutMapping("/drm-info/update")
    @Operation(summary = "更新项目资源设备信息表 ")
    @PreAuthorize("@ss.hasPermission('obj:base-info:update')")
    public CommonResult<Boolean> updateDrmInfo(@Valid @RequestBody ObjDrmInfoDO drmInfo) {
        baseInfoService.updateDrmInfo(drmInfo);
        return success(true);
    }

    @DeleteMapping("/drm-info/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除项目资源设备信息表 ")
    @PreAuthorize("@ss.hasPermission('obj:base-info:delete')")
    public CommonResult<Boolean> deleteDrmInfo(@RequestParam("id") Long id) {
        baseInfoService.deleteDrmInfo(id);
        return success(true);
    }

	@GetMapping("/drm-info/get")
	@Operation(summary = "获得项目资源设备信息表 ")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('obj:base-info:query')")
	public CommonResult<ObjDrmInfoDO> getDrmInfo(@RequestParam("id") Long id) {
	    return success(baseInfoService.getDrmInfo(id));
	}

    // ==================== 子表（项目实施计划信息表 ） ====================

    @GetMapping("/plan-info/page")
    @Operation(summary = "获得项目实施计划信息表 分页")
    @Parameter(name = "xmid", description = "项目编号")
    @PreAuthorize("@ss.hasPermission('obj:base-info:query')")
    public CommonResult<PageResult<ObjPlanInfoDO>> getPlanInfoPage(PageParam pageReqVO,
                                                                                        @RequestParam("xmid") String xmid) {
        return success(baseInfoService.getPlanInfoPage(pageReqVO, xmid));
    }

    @PostMapping("/plan-info/create")
    @Operation(summary = "创建项目实施计划信息表 ")
    @PreAuthorize("@ss.hasPermission('obj:base-info:create')")
    public CommonResult<Long> createPlanInfo(@Valid @RequestBody ObjPlanInfoDO planInfo) {
        return success(baseInfoService.createPlanInfo(planInfo));
    }

    @PutMapping("/plan-info/update")
    @Operation(summary = "更新项目实施计划信息表 ")
    @PreAuthorize("@ss.hasPermission('obj:base-info:update')")
    public CommonResult<Boolean> updatePlanInfo(@Valid @RequestBody ObjPlanInfoDO planInfo) {
        baseInfoService.updatePlanInfo(planInfo);
        return success(true);
    }

    @DeleteMapping("/plan-info/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除项目实施计划信息表 ")
    @PreAuthorize("@ss.hasPermission('obj:base-info:delete')")
    public CommonResult<Boolean> deletePlanInfo(@RequestParam("id") Long id) {
        baseInfoService.deletePlanInfo(id);
        return success(true);
    }

	@GetMapping("/plan-info/get")
	@Operation(summary = "获得项目实施计划信息表 ")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('obj:base-info:query')")
	public CommonResult<ObjPlanInfoDO> getPlanInfo(@RequestParam("id") Long id) {
	    return success(baseInfoService.getPlanInfo(id));
	}

}